package tools

import (
	"fmt"
	"strconv"
	"strings"
)

type StockTool struct{}

//wowCoupon:1000
func (s *StockTool) GenRedisKeyBusiness(business_id int64) string {
	res := "wowCoupon" + ":" + fmt.Sprint(business_id)
	return res
}

//JD1008989900:100:stock
func (s *StockTool) GenRedisHashKeyStock(good_id, id int64) string {
	res := fmt.Sprint(good_id) + ":" + fmt.Sprint(id) + ":" + "stock"
	return res
}

//JD1008989900:100:is
func (s *StockTool) GenRedisHashKeyIssueStartTime(good_id, id int64) string {
	res := fmt.Sprint(good_id) + ":" + fmt.Sprint(id) + ":" + "is"
	return res
}

//JD1008989900:100:ie
func (s *StockTool) GenRedisHashKeyIssueEndTime(good_id, id int64) string {
	res := fmt.Sprint(good_id) + ":" + fmt.Sprint(id) + ":" + "ie"
	return res
}

//RobberList:1000:JD1008989900:100
func (s *StockTool) GenRedisKeyRobberList(business_id, good_id, id int64) string {
	res := "RobberList:" + fmt.Sprint(business_id) + ":" + fmt.Sprint(good_id) + ":" + fmt.Sprint(id)
	return res
}

//JD1008989900:100:ie
func (s *StockTool) GetGoodIDMetaID(key string) (good_id int64, id int64) {
	vals := strings.Split(key, ":")

	if len(vals) < 2 {
		return 0, 0
	}

	good_id, err := strconv.ParseInt(vals[0], 10, 64)
	if err != nil {
		return 0, 0
	}

	id, err = strconv.ParseInt(vals[1], 10, 64)
	if err != nil {
		return 0, 0
	}

	return good_id, id
}

//JD1008989900:100:ie
func (s *StockTool) GetBusinessID(key string) (businessID int64) {
	vals := strings.Split(key, ":")

	if len(vals) < 2 {
		return 0
	}

	businessID, err := strconv.ParseInt(vals[1], 10, 64)
	if err != nil {
		return 0
	}

	return businessID
}
